package com.finsiot.tools.taos.migration;

import com.finsiot.tools.taos.migration.bean.DataBase;
import com.finsiot.tools.taos.migration.bean.DataTypeEnum;
import com.finsiot.tools.taos.util.Constant;
import com.finsiot.tools.taos.util.ParameterUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Hello world!
 *
 */
@Slf4j
public class ImportApp
{

    private static Pattern regex = Pattern.compile( "^[1-9]\\d*$");
    private static String filePath;
    public static void main( String[] args ) throws SQLException, IOException {


        CommandLineParser commandLineParser = new DefaultParser();
        Options options = new Options();
        options.addOption(Constant.HOST, "192.168.110.254",true, "数据库地址");
        options.addOption(Constant.PORT, "6041",true, "数据库端口");
        options.addOption(Constant.DATABASE,  "alinks",true,"数据库名称");
        options.addOption(Constant.USERNAME, "root",true,"用户名");
        options.addOption(Constant.PASSWORD, "taosdata",true, "用户密码");
        options.addOption(Constant.KEEP,  "365",true,"保存数据天数");
        options.addOption(Constant.PRECISION,  "ms",true,"时间精度：ms 毫秒，us 微妙，ns 纳秒");
        options.addOption(Constant.CONTENT, "all",true, "内容：meta 元数据，data 数据，all 元数据和数据一起");
        options.addOption(Constant.DATA_PATH, "/Users/mac/test2",true, "导出数据的存放目录");

        if(args.length<16){
            System.out.println(ParameterUtils.getHelp(options));
        }



        CommandLine commandLine=null;
        try {
            commandLine = commandLineParser.parse(options, args);

        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println(ParameterUtils.getHelp(options));
            System.exit(1);
        }

        String host=commandLine.getOptionValue(Constant.HOST);
        Integer port=Integer.parseInt(commandLine.getOptionValue(Constant.PORT));
        String database=commandLine.getOptionValue(Constant.DATABASE);
        String username=commandLine.getOptionValue(Constant.USERNAME);
        String password=commandLine.getOptionValue(Constant.PASSWORD);
        Integer keep=Integer.parseInt(commandLine.getOptionValue(Constant.KEEP));
        String precision=commandLine.getOptionValue(Constant.PRECISION);

        String content=commandLine.getOptionValue(Constant.CONTENT);
        if(content==null){
            content="meta";
        }
        DataTypeEnum dataTypeEnum=DataTypeEnum.valueOf(content);
        filePath=commandLine.getOptionValue(Constant.DATA_PATH);

        DataBase dataBase=new DataBase(host,port,database,username,password,precision,keep);

        ImportData importData=new ImportData(dataBase);
        long start=System.currentTimeMillis();
        long end;
        long westTime;
        switch (dataTypeEnum){
            case all:

                log.info("导入表结构开始");
                importData.importMeta(filePath);
                end=System.currentTimeMillis();

                westTime=(end-start)/1000;
                log.info("导入数据耗时：{}分钟{}秒",(westTime/60),(westTime%60));

                importData.importData(filePath);
                break;
            case meta:
                log.info("导入数据开始");
                importData.importMeta(filePath);
                end=System.currentTimeMillis();

                westTime=(end-start)/1000;
                log.info("导入数据耗时：{}分钟{}秒",(westTime/60),(westTime%60));
                break;
            case data:

                importData.importData(filePath);
                break;
            default:
        }

    }
}
